import sys
import os
import os.path
import shutil

import mdf.debug as debug
import mdf.config as config
import mdf.utils as utils

#IF Not EXcist CReate
def ifnexcr( path ):
	if not os.path.exists( path ):
		os.mkdir( path )
		return True
	return False

#add support of if no changes then dont recopy all
class OutputDir:
	def __init__( self ):
		self.def_dirs = ["","/css","/rss","/files","/files/writeup","/images",
"/images/writeup","/js","/js/sh","/js/fancybox","/posts","/writeup"]
		#create initial output file directories
		for el in self.def_dirs:
			try:
				ifnexcr( config.default_output_dir+el )
			except:
				print "Cannot create dir ", config.default_output_dir+el
	
	def prepare_dirs( self ):
		
		pass
	
	#no more then 2 levels at this moment
	def copy_files( self, path=None ):
		if path == None:
			for diren in os.listdir( config.default_content_dir ):
				if os.path.isfile( config.default_content_dir+"/"+diren ):
					try:
						shutil.copy( config.default_content_dir+"/"+diren, config.default_output_dir+"/"+diren )
					except:
						print "Cannot copy ", config.default_content_dir+"/"+diren
				elif os.path.isdir( config.default_content_dir+"/"+diren ):
					self.copy_files( path= "/"+diren)
		elif path != None:
			for diren in os.listdir( config.default_content_dir+"/"+path ):
				if os.path.isfile( config.default_content_dir+path+"/"+diren ):
					try:
						shutil.copy( config.default_content_dir+path+"/"+diren, config.default_output_dir+path+"/"+diren )
					except:
						print "Cannot copy ", config.default_output_dir+path+"/"+diren
				elif os.path.isdir( config.default_content_dir+"/"+path ):
					self.copy_files( path= path+"/"+diren )
